package amit.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.StringTokenizer;

import amit.ad.AdDetails;

public class FileUtil {
	
	public static final String DATA_DIR = System.getProperty("data.dir", 
			"C:/temp/amitRentData/");
	
	public static String DELIMITER = ",";
	public static FileUtil sharedInstance = new FileUtil();

	private FileUtil() {
	}

	/**
	 * 
	 * @param filename
	 * @return
	 */
	public String readTextFile(String filename) throws Exception {
		StringBuffer allContent = new StringBuffer("");
		String str;
		BufferedReader in = new BufferedReader(new FileReader(filename));
		while ((str =  in.readLine()) != null) {
			allContent.append(str);
			allContent.append(DELIMITER);
		}
		in.close();
		return allContent.toString();
	}

	/**
	 * ????,3.0,125,?????,??,20/10/2006
	 * @param filename
	 * @return
	 * @throws Exception
	 */
	public ArrayList<AdDetails> readAdFile(String filename) throws Exception {
		ArrayList<AdDetails> ads = new ArrayList<AdDetails>();
		String str;
		FileInputStream fis = new FileInputStream(filename);
		BufferedReader in = new BufferedReader(new InputStreamReader(fis,"UTF-8"));
				//new FileReader(filename));
		while ( (str = in.readLine()  ) != null) {
			//String hebStr = new String(str.getBytes("UTF8"));
			//System.out.println("=== str:"+str);
			try {
				StringTokenizer st = new StringTokenizer(str, ",");
				ads.add(new AdDetails(st.nextToken(), st.nextToken(), Float.parseFloat(st.nextToken()),
						Integer.parseInt(st.nextToken()), st.nextToken(), 
						st.nextToken(), st.nextToken() ,true ));
			} catch (Exception e) {
				System.out.println("ERR on parsing of:"+str+ " in file:"+filename);
			}
		}
		in.close();
		return ads;
	}

	/**
	 * 
	 * @param fileName
	 * @param data
	 * @throws Exception
	 */
	public void writeToFile(String fileName, ArrayList<String> data)
			throws Exception {
		BufferedWriter out = new BufferedWriter(new FileWriter(fileName));
		for (int i = 0; i < data.size(); i++) {
			out.write(data.get(i) + "\n");
		}
		out.close();
	}

	public void writeToFile(String fileName, String data)  {
		try {
			BufferedWriter out = new BufferedWriter(new FileWriter(fileName));
			out.write(data + "\n");
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 
	 * @param fileName
	 * @param record -
	 *            a string with , as dilimiter
	 * @throws Exception
	 */
	public synchronized void writeToCSVFile(String fileName, String record) throws Exception {
		boolean append = new File(fileName).exists();
		FileOutputStream fos = new FileOutputStream(fileName, append);
		BufferedWriter out = new BufferedWriter(new OutputStreamWriter(fos, "UTF8"));
		out.write(record);
		out.newLine();
		out.close();
	}

}
